<?php

namespace app\common\service;

use app\common\service\Base;
use think\Db;

/**
 * 成本盈亏报表
 */
class CostStatement extends Base
{
    //加工费
    public function  processCost ( $order_id,$sku ) {
        $skuInfo = db('sku')->field('price')->where('sku',$sku)->find();
        $costCheckInfo = $this->costCheck($order_id);
        $order_size = $this->orderSize($order_id);
        $price = $skuInfo['price'];//原加工费
        $check_price = 0;//核价
        if($costCheckInfo['param1'] > 0 && $costCheckInfo['param2'] == 0){
            $check_price = $costCheckInfo['param1'];
        }
        if($costCheckInfo['param2'] > 0 ){
            $check_price = $costCheckInfo['param2'];
        }
        $order_count = array_sum(array_column($order_size,'order_count'));//下单数
        $shicai_count = array_sum(array_column($order_size,'shicai_count'));//裁剪数
        $store_count = array_sum(array_column($order_size,'store_count'));//MES入仓数
        //核价差异
        $cen_check_price = 0;
        $check_price_total = 0;
        if($check_price>0){
            $_check_price_int = (int)$check_price*100 - (int)$price*100;
            $cen_check_price = round($_check_price_int/100,2);
            $check_price_total = round(($_check_price_int * $shicai_count)/100,2);//裁剪总金额
        }
        //款式工价
        $_sku_process_price = db('sku_process_price')->field('sku,process_price')->where('sku',$sku)->select();
        $sku_process_price_total = array_sum(array_column($_sku_process_price,'process_price'));//款式工价
        //款式工价占比
        $_order_ext_info = db('order_ext')->field('cost')->where('order_id',$order_id)->find();
        $cost = $_order_ext_info['cost'];
        $cost_pre = 100.00;
        if($cost > 0){
            //款式工价赞比
            $cost_pre = sprintf("%.2f",$sku_process_price_total/$cost *100);
        }
        //盈亏情况 = （核价后加工费 - （核价后加工费 * 设置值）） - （核价后加工费 - 工厂总单价） * MES入仓数
        //判断是否亏损，无设置值，或者为 0 的情况下。默认为 无亏损
        $config = $this->getCostStatementConfig();
        $is_profit = 0;//亏损
        if($config['process_cost_status'] != 1 || $config['process_cost'] == 0){
            $is_profit= 1;//无亏损
        }
        $process_cost =  sprintf("%.2f",$config['process_cost'] /100);
        $profit_total = ($check_price_total - $check_price_total * $process_cost) - ($check_price - $sku_process_price_total) * $store_count;//盈亏情况
        if( $profit_total>0){
            $is_profit= 1;//无亏损
        }
        return ['price'=>$price,'check_price'=>$check_price,'cen_check_price'=>$cen_check_price,
            'order_count'=>$order_count,'store_count'=>$store_count,'shicai_count'=>$shicai_count,
            'check_price_total'=>$check_price_total,'sku_process_price_total'=>$sku_process_price_total,
            'cost_pre'=>$cost_pre,'profit_total'=>$profit_total,'is_profit'=>$is_profit];
    }

    //面辅料
    public function  accessories ( $order_id ) {

    }

    //用量
    public function consumption ( $order_id ) {

    }

    //二次工艺
    public function secondaryProcess( $order_id ) {

    }

    //补损
    public function makeLoss ( $order_id ){

    }

    //正品盈亏
    public function genuineProfit ( $order_id ){

    }

    //获取sku信息

    //核价通过数据
    public function costCheck($order_id){
        $costCheckInfo = [];
        $costCheckInfo = db('cost_check')->field('param1,param2')->where('order_id',$order_id)->where('status',100)->find();
        return $costCheckInfo;
    }
    //实裁数
    public function orderSize( $order_id ){
        $size_field = 'mid,order_id,factory_id,ordernum,client_ordernum,size_name,order_count,shicai_count,store_count';
        $order_size = db('order_size')->field($size_field)->where('order_id',$order_id)->select();
        return $order_size;
    }
    //保存配置文件，如果存在配置文件，则覆盖
    public function addCostStatementConfig($content){
        //先查询
        $config = db('cost_statement_config')->limit(1)->select();
        $data['update_time'] = time();
        $data['admin_id'] =  ADMIN_ID;
        $data['content'] = json_encode($content);
        if(empty($config)){
            $data['create_time'] = time();
            $info = db('cost_statement_config')->insert($data);
        }else{
            $info = db('cost_statement_config')->where('id',$config[0]['id'])->update($data);
        }
        return $info;
    }
    //读取配置文件
    public function getCostStatementConfig(){
        $config = db('cost_statement_config')->limit(1)->select();
        $info = [];
        if(!empty($config)){
            $content = $config[0]['content'];
            $info = json_decode($content,true);
        }
        return $info;
    }

    //保存成本盈亏表
    public function insertOrUpdateCostStatement( $data ){
        if($data['id']){

        }
    }
}